#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

#define fatal(test) do {if (test) {printf ("trash: %s\n", strerror(errno)); \
                       exit(EXIT_FAILURE);}} while(0)

int
main (int argc, char **argv)
{
  int pid, pid2, status;
  int f, c;


  printf ("Starting...\n");

  f = 2;
  c = 4;

  pid = fork ();
  fatal (pid<0);
 
  if (pid>0)
    {
      while (f--)
        {
          printf ("%d F: (pid %5d, father pid %d) my pid %d\n", f, pid, getppid(), getpid());
          sleep(1);
	  pid2 = fork();	
	  printf ("\t%d F: (pid %5d, CHILD pid %d) my pid %d\n", f, pid2, getppid(), getpid());
        }
     
      printf ("F - waiting for child\n");
      wait (&status);
      printf ("F - my child finished with status %d\n", status);
      sleep(4);
      printf ("* Father finished\n");

    }
  else
    {
      while (c--)
        {
          printf ("%d c: (pid %5d, father pid %d) my pid %d\n", c, pid, getppid(), getpid());
          sleep(1);
        }
      printf ("* Child finished\n");

    }
 

  return EXIT_SUCCESS;
}

/*Show details Hide details

Change log
r10 by renesp on Mar 17, 2009   Diff

Adicao do exemplo de fork do prof. Monaco

Go to: 	

Older revisions
All revisions of this file

File info
Size: 981 bytes, 55 lines
View raw file*/

